using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AntEater.ComponentModel;

namespace AntEater.Runtime.UI
{
    /// <summary>
    /// View for presenting a selector for build files
    /// </summary>
    internal partial class BuildFileSelectorView : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BuildFileSelectorView"/> class.
        /// </summary>
        public BuildFileSelectorView() {
            InitializeComponent();
        }

        private Solution _solution;

        /// <summary>
        /// Gets or sets the solution.
        /// </summary>
        /// <value>The solution.</value>
        public Solution Solution {
            get { return _solution; }
            set {
                _solution = value;
                this.Refresh();
            }
        }

        private BuildFile _selectedBuildFile;

        /// <summary>
        /// Gets the selected build file.
        /// </summary>
        /// <value>The selected build file.</value>
        public BuildFile SelectedBuildFile {
            get { return _selectedBuildFile; }
        }

        /// <summary>
        /// Forces the control to invalidate its client area and immediately redraw itself and any child controls.
        /// </summary>
        /// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
        public override void Refresh() {

            // Sort build files by name
            SortedList<string, BuildFile> buildFiles = new SortedList<string, BuildFile>(StringComparer.InvariantCultureIgnoreCase);
            foreach (BuildFile buildFile in _solution.BuildFiles) {
                buildFiles.Add(buildFile.Name, buildFile);
            }

            // Add Items
            buildFileListView.Items.Clear();
            foreach (BuildFile buildFile in buildFiles.Values) {
                ListViewItem item = buildFileListView.Items.Add(buildFile.Name, "BuildFile");
                item.Tag = buildFile;
            }

            base.Refresh();
        }

        /// <summary>
        /// Oks the clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OkClicked(object sender, EventArgs e) {

            if (_selectedBuildFile != null) {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        /// <summary>
        /// Cancels the clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CancelClicked(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Items the selection changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.ListViewItemSelectionChangedEventArgs"/> instance containing the event data.</param>
        private void ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
            if (e.IsSelected) {
                _selectedBuildFile = (BuildFile)e.Item.Tag;
            }
            okButton.Enabled = e.IsSelected;
        }

        /// <summary>
        /// Lists the double clicked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ListDoubleClicked(object sender, EventArgs e) {

            if (_selectedBuildFile != null) {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}